﻿using System;
using System.Linq;
using System.Data.Entity;
using System.Collections.Generic;
using Rainr.EFClient;

namespace ConsoleApplication
{
    /*
        实体类
     */
    public class TestUser
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }


    /*
        数据上下文
     */
    public class TestContext : DbContext
    {
        public TestContext() : base("TextConnection") { }

        public DbSet<TestUser> TestUser { get; set; } //确保名称和类别一致，如果不一致无法更新非代理对象
    }


    /*
       使用演示
     */
    class Program
    {
        static void Main(string[] args)
        {
            //访问者
            DbAccessor<TestContext> accessor = new DbAccessor<TestContext>(); // == accessor = DbAccessor.NewAccessor<TestContext>();

            //-----查询------//
            TestUser selectUser = null;
            List<TestUser> selectUsers = null;
            //根据主键值查询单个对象
            selectUser = accessor.SelectByKey<TestUser>(1);
            //根据参数查询单个对象
            selectUser = accessor.Select<TestUser>(u => u.Id == 1);
            //查询多个对象
            selectUsers = accessor.Selects<TestUser>().ToList();
            //根据参数查询多个对象
            selectUsers = accessor.Selects<TestUser>(u => u.Id > 0 && u.Name == "testName").ToList();
            //无跟踪，排序，分页，查询多个对象
            int rowTotal = 0;
            selectUsers = accessor.Selects<TestUser>(u => u.Id > 0).OrderBy("Id").Pagination(1, 20, out rowTotal).AsNoTracking().ToList();

            //-----插入------//
            TestUser insertUser = new TestUser() { Name = "insert.." };
            //插入对象
            accessor.Insert<TestUser>(insertUser);

            //-----更新------//
            TestUser updateProxyUser = accessor.SelectByKey<TestUser>(1);
            TestUser updateUser = new TestUser() { Id = 1, Name = "uuuuu" };
            //更新代理对象
            accessor.Update<TestUser>(updateProxyUser);
            //更新非代理对象
            accessor.Update<TestUser>(updateUser);
            //更新对象某些属性
            accessor.UpdateProperty<TestUser>(updateUser, "Name", "Age");//..可指定多个
            //更新对象属性排除某些属性
            accessor.UpdatePropertyExcluded<TestUser>(updateUser, "Name", "Age");//..可指定多个

            //-----删除------//
            TestUser deleteProxyUser = accessor.SelectByKey<TestUser>(1);
            TestUser deleteUser = new TestUser() { Id = 1, Name = "dddd" };
            //根据主键删除对象
            accessor.DeleteByKey<TestUser>(1);
            //删除代理对象
            accessor.Delete<TestUser>(deleteProxyUser);
            //删除非代理对象
            accessor.Delete<TestUser>(deleteUser);

            //-----直接Linq 操作------//
            var linqUsers = (from users in accessor.Context.TestUser select users);

            //-----事务------//
            //开始事务
            accessor.BeginTransaction();
            //中间多个 操作。。。。
            //提交事务
            accessor.SubmitTransaction();

            //-----分布式事务：有待测试------//
            DbAccessor<TestContext> accessone = new DbAccessor<TestContext>();
            DbAccessor<OtherContext> accesstwo = new DbAccessor<OtherContext>();
            DbAccessorTransaction dba = new DbAccessorTransaction();
            dba.EnlistAccessor<DbAccessor<TestContext>>(accessone);//加入访问第一个数据库的访问器
            dba.EnlistAccessor<DbAccessor<OtherContext>>(accesstwo);//加入访问别的数据库的访问器
            dba.SubmitTransaction();//提交分布式事务

        }
    }
}
